import { GCellRenderer } from "../../../../com/mui/controls/GCellRenderer";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { ResFileConst } from "../../resource/ResFileConst";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { IllustratePieceInfo } from "./IllustratePieceInfo";
import { ItemData } from "../../resource/info/ItemData";
import { ColorConfig } from "../../resource/ColorConfig";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { LoaderPriority } from "../../../../com/gengine/resource/LoaderPriority";
import { IllustrateState } from "./IllustrateState";
import { TweenLite } from "../../../../com/greensock/TweenLite";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { Alert } from "../../../../com/mui/controls/Alert";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { GLoadedButton } from "../../../../com/mui/controls/GLoadedButton";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
type int = number;
//class IllustratePieceRenderer
    
    export  class IllustratePieceRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            return;
        }

        protected /* override */ configUI(): void
        {
            super.configUI();
            buttonMode = true;
            useHandCursor = true;
            ObjCreate.createBg(10, 0, 100, 20, this, ImagesConst.SelectedBg);
            this._nameTxt = ObjCreate.createGTextField("", 0, 0, 113, 20, this, GlobalStyle.getTextFormatCenter(GlobalStyle.textFormat1));
            this._itemBmp = new Object /* flash.display.Bitmap */();
            this._itemBmp.x = 5;
            this._itemBmp.y = 27;
            addChild(this._itemBmp);
            this._itemBorderBg = new Object /* flash.display.Bitmap */();
            this._itemBorderBg.y = 22;
            addChild(this._itemBorderBg);
            this._cardBtn = ObjCreate.createGLoadedButton(ResFileConst.illustrateCardBtn, 0, 22, 113, 146, this);
            addEventListener(flash.events.MouseEvent.CLICK, this.onMouseClickHandler);
            LoaderHelp.addResCallBack(ResFileConst.Illustrate, this.onResLoaded);
            return;
        }

        /* internal  */onResLoaded(): void
        {
            this._itemBorderBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.illustrateCardBg);
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            var loc1=0;
            super.data = arg1;
            this._itemBmp.bitmapData = null;
            this._pieceInfo = arg1 as IllustratePieceInfo;
            if (this._cardBtn != null) 
            {
                addChild(this._cardBtn);
            }
            else 
            {
                this._cardBtn = ObjCreate.createGLoadedButton(ResFileConst.illustrateCardBtn, 0, 22, 113, 146, this);
            }
            if (this._activeBtn && this._activeBtn.parent) 
            {
                removeChild(this._activeBtn);
            }
            if (this._pieceInfo) 
            {
                loc1 = this._pieceInfo.code;
                if (loc1 != 0) 
                {
                    this._itemData = new ItemData(loc1);
                    this._color = ColorConfig.instance.getItemColor(this._itemData.itemInfo.item.color).color;
                    this._nameTxt.htmlText = "<font color=\'" + this._color + "\'>" + this._itemData.itemInfo.item.name + "</font>";
                    this._imagesName = this._itemData.itemInfo.item.effectEx + ".jpg";
                    this._itemBmp.visible = false;
                    LoaderManager.instance.load(this._imagesName, this.onLoadCompleteHandler, LoaderPriority.LevelA);
                }
                if (this._pieceInfo.state == IllustrateState.IllustrateCanActiveBlue || this._pieceInfo.state == IllustrateState.IllustrateCanActivePurple || this._pieceInfo.state == IllustrateState.IllustrateCanActiveOrange) 
                {
                    if (this._activeBtn != null) 
                    {
                        addChild(this._activeBtn);
                    }
                    else 
                    {
                        this._activeBtn = ObjCreate.createGLoadedButton(ResFileConst.illustrateActiveBtn, 27, 22, 86, 90, this);
                        this._activeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onActiveBtnClickHandler);
                    }
                }
            }
            return;
        }

        /* internal  */playFlash(): void
        {
            this._itemBmp.visible = true;
            if (this._cardBtn) 
            {
                TweenLite.to(this._cardBtn, 0.5, {"rotation": 180, "x": this.width / 2, "y": this.height / 2, "scaleX": 0, "scaleY": 0, "scaleZ": 0, "onComplete": this.onCompleteHandler});
            }
            if (this._activeBtn) 
            {
                TweenLite.to(this._activeBtn, 0.5, {"rotation": 180, "x": this.width / 2, "y": this.height / 2, "scaleX": 0, "scaleY": 0, "scaleZ": 0});
            }
            return;
        }

        /* internal  */onCompleteHandler(): void
        {
            this.removeCard();
            Dispatcher.dispatchEvent(new DataEvent(EventName.IllustratePieceActive, this._pieceInfo.uid));
            return;
        }

        /* internal  */removeCard(): void
        {
            if (this._cardBtn && this._cardBtn.parent) 
            {
                removeChild(this._cardBtn);
                this._cardBtn = null;
            }
            if (this._activeBtn && this._activeBtn.parent) 
            {
                removeChild(this._activeBtn);
                this._activeBtn = null;
            }
            return;
        }

        /* internal  */onLoadCompleteHandler(arg1: ImageInfo): void
        {
            if (arg1.name == this._imagesName) 
            {
                this._itemBmp.bitmapData = arg1.bitmapData;
            }
            if (this._pieceInfo.state == IllustrateState.IllustrateHadActive || this._pieceInfo.state == IllustrateState.IllustrateCanActivePurple || this._pieceInfo.state == IllustrateState.IllustrateCanActiveOrange) 
            {
                if (this._cardBtn && this._cardBtn.parent) 
                {
                    removeChild(this._cardBtn);
                }
                this._itemBmp.visible = true;
            }
            return;
        }

        /* internal  */onMouseClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._pieceInfo && this._pieceInfo.state == IllustrateState.IllustrateCanActiveBlue) 
            {
                this.playFlash();
            }
            return;
        }

        /* internal  */onActiveBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._activeBtn && this._pieceInfo && !(this._pieceInfo.state == IllustrateState.IllustrateHadActive) && !(this._pieceInfo.state == IllustrateState.IllustrateNotActive)) 
            {
                TweenLite.to(this._activeBtn, 0.5, {"rotation": 180, "x": this.width / 2, "y": this.height / 2, "scaleX": 0, "scaleY": 0, "scaleZ": 0, "onComplete": this.onCompleteHandler});
            }
            return;
        }

        /* internal  */onCloseHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.IllustratePieceActive, this._pieceInfo.uid));
            }
            return;
        }

        public /* override */ set selected(arg1: Boolean)
        {
            super.selected = arg1;
            this._itemBorderBg.filters = [];
            if (this._cardBtn && this._cardBtn.parent) 
            {
                this._cardBtn.filters = [];
            }
            if (this._pieceInfo && arg1) 
            {
                if (!(this._pieceInfo.state == IllustrateState.IllustrateNotActive) && !(this._pieceInfo.state == IllustrateState.IllustrateCanActiveBlue)) 
                {
                    this._itemBorderBg.filters = [FilterConst.itemChooseFilter];
                }
                else if (this._cardBtn) 
                {
                    this._cardBtn.filters = [FilterConst.itemChooseFilter];
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.IllustratePieceSelectChange, this._pieceInfo));
            }
            return;
        }

        /* internal */ /* var */_pieceInfo: IllustratePieceInfo;

        /* internal */ /* var */_itemBmp: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_itemBorderBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_cardBtn: GLoadedButton;

        /* internal */ /* var */_activeBtn: GLoadedButton;

        /* internal */ /* var */_itemData: ItemData;

        /* internal */ /* var */_nameTxt: GTextFiled;

        /* internal */ /* var */_color: string="";

        /* internal */ /* var */_imagesName: string="";
    }
